/* support both new and old IR remote controller */

#include <stc8g.h>
#include "intrins.h"

#define ON 0
#define OFF 1


sbit INIR = P3^2; // IR signal input
//sbit P34 = P3^4;  //led Green for stc15f104w


#define P5PU  (*(unsigned char volatile xdata *)0xfe15)

typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;

bit IR_int = 0;
bit valid=0;

unsigned char Data[4] = {0};
void InFrared_Init(void);


unsigned int On_length=255; 
/* On_length = 255 always on ;
							 = 30 0.5h ;
							 = 60 1h ;
							 = 120 2h ;
							 = 240 4h ;	
							 = 480 8h ;	
*/


unsigned char slot=0;
unsigned char sec=0;
unsigned int min=0;
unsigned int count=0;

void timer1_ISR(void) interrupt 3 
{

	slot+=1;
	if (slot==14) { slot=0; sec+=1;} //1s 
	if (sec==60)  {min+=1;sec=0;}              //1min
	if (On_length==255)                        //press on or off button
			{TR1 = 0;min=0;sec=0;slot=0;}
	else
			{ 
				if (min==On_length)  {TR1 = 0; min=0;sec=0;slot=0;P55=OFF;}	// timer is out 
			}
}



void main(void)
{

		   P3M0 = 0x10;//p32 input ;                p34 out
       P3M1 = 0x04;
	 //    P5M0 = 0x02;//P55 work in strong output mode
   //    P5M1 = 0x00;
       P5M0 &= ~0x20; P5M1 &= ~0x20; 
	     P_SW2 |= 0x80;                              //enable XFR register access function
       P5PU &= ~0x20;  //P55 enble 4.7k resistor

		   //P34=ON;//after power on default switch on .
	     P55=ON;//after power on default switch on 
	     TMOD = 0x01; // Timer0 work at mode2;timer1 work at mode0 16bit autoreload
	    // AUXR |= 0x14;//timer1 working at 1T mode
                  

	  
      TL1 = 0x00;                                 //65536-11.0592M/12/1000
      TH1 = 0x00;
      TR1 = 1;                                    //?????
      ET1 = 1;                                    //???????
      
       //PT1 = 1;     //improve timer1 interrupt priority
			 IT0 = 1;   // 1: only down-edge ; 0: both up and down edge.
			 EX0 = 1;
       EA = 1;
	 
       while (1)
       {
			 									 
				if (IR_int == 1)
              {
                     EX0 = 0;// close ex0 int
                     InFrared_Init();
										//80 ff 0a f5  on power 
										//80 ff 0d f2  off mute
										//80 ff 11 ee  0.5h  1
										//80 ff 12 ed  1h   2
										//80 ff 13 ec  2h  3
										//80 ff 14 eb  4h  4 
										//80 ff 15 ea  8h  5 

										if((Data[0]==0x80)&&(Data[1]==0xff)&&(Data[2]==0x0a)&&(Data[3]==0xf5))  
												{P55=ON; TR1 = 0; On_length=255;} //on
										if((Data[0]==0x80)&&(Data[1]==0xff)&&(Data[2]==0x0d)&&(Data[3]==0xf2))  
												{P55=OFF; TR1 = 0; On_length=255;} //off			
										if((Data[0]==0x80)&&(Data[1]==0xff)&&(Data[2]==0x11)&&(Data[3]==0xee))  
												{P55=ON;On_length=30;TR1 = 1;min=0;sec=0;slot=0;} //0.5h
										if((Data[0]==0x80)&&(Data[1]==0xff)&&(Data[2]==0x12)&&(Data[3]==0xed))  
												{P55=ON;On_length=60;TR1 = 1;min=0;sec=0;slot=0;} //1h
										if((Data[0]==0x80)&&(Data[1]==0xff)&&(Data[2]==0x13)&&(Data[3]==0xec))  
												{P55=ON;On_length=120;TR1 = 1;min=0;sec=0;slot=0;} //2h	
										if((Data[0]==0x80)&&(Data[1]==0xff)&&(Data[2]==0x14)&&(Data[3]==0xeb))  
												{P55=ON;On_length=240;TR1 = 1;min=0;sec=0;slot=0;} //4h													
										if((Data[0]==0x80)&&(Data[1]==0xff)&&(Data[2]==0x15)&&(Data[3]==0xea))  
												{P55=ON;On_length=480;TR1 = 1;min=0;sec=0;slot=0;} //8h																				
										
										//POWER 40 2c 45 ba
										//MUTE  40 2c 41 be 
										//1     40 2c 09 f6
										//2     40 2c 1d e2
										//3     40 2c 1f e0 
										//4     40 2c 0d f2 
										//5     40 2c 19 e6
										if((Data[0]==0x40)&&(Data[1]==0x2c)&&(Data[2]==0x45)&&(Data[3]==0xba))  
												{P55=ON; TR1 = 0; On_length=255;} //on
										if((Data[0]==0x40)&&(Data[1]==0x2c)&&(Data[2]==0x41)&&(Data[3]==0xbe))  
												{P55=OFF; TR1 = 0; On_length=255;} //off			
										if((Data[0]==0x40)&&(Data[1]==0x2c)&&(Data[2]==0x09)&&(Data[3]==0xf6))  
												{P55=ON; On_length=30;TR1 = 1;min=0;sec=0;slot=0;} //0.5h
										if((Data[0]==0x40)&&(Data[1]==0x2c)&&(Data[2]==0x1d)&&(Data[3]==0xe2))  
												{P55=ON; On_length=60;TR1 = 1;min=0;sec=0;slot=0;} //1h
										if((Data[0]==0x40)&&(Data[1]==0x2c)&&(Data[2]==0x1f)&&(Data[3]==0xe0))  
												{P55=ON; On_length=120;TR1 = 1;min=0;sec=0;slot=0;} //2h	
										if((Data[0]==0x40)&&(Data[1]==0x2c)&&(Data[2]==0x0d)&&(Data[3]==0xf2))  
												{P55=ON; On_length=240;TR1 = 1;min=0;sec=0;slot=0;} //4h													
										if((Data[0]==0x40)&&(Data[1]==0x2c)&&(Data[2]==0x19)&&(Data[3]==0xe6))  
												{P55=ON; On_length=480;TR1 = 1;min=0;sec=0;slot=0;} //8h	
									
                TR0 = 0; 
								EX0 = 1;
                IR_int = 0;
							}
       }
}

void Int_0(void) interrupt 0
{
       IR_int  = 1; //indicate occur int.
}

void InFrared_Init(void)
{
       unsigned char i, j;
       TH0 = 0;
       TL0 = 0;
	     TR0 = 1; //start timer0 to count 
       while (INIR == 0 && TH0 <= 35);//sys clk =11.0592M, after 12DIV sysclk=0.9216M, T=1.085us ;so 9ms about 8294xT=32.3x256T
			 if (INIR == 1)
       {

            while (INIR == 1 && TH0 <= 55);		//9+4.5ms=13.5ms  about 12442T= 48.6 x256T
              if(INIR==0 &&TH0>=48){   
				     for (i = 0; i < 4; i++)
              {
                     for (j = 0; j < 8; j++)
                     {
                            TH0 = 0;
                            TL0 = 0;
                            while (TH0 <= 2)
														{if( INIR ==1) break ;}
														while (INIR == 1);
                            Data[i] >>= 1;
                            if (TH0 >= 7)
                            {
                             Data[i] |= 0x80;
                            }
                     }
              }
						}
			 TR0 = 0; //start timer0 to count 
				}
		}
